package fei.typhoon.engine.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class FeiConfiger {

	public static Properties properties = new Properties();

	public static void setResource(Class<?> c, String src) {
		if (!properties.isEmpty()) {
			properties.clear();
		}
		byte[] resource = getResourceBytes(c, src);
		if (resource != null) {
			ByteArrayInputStream bis = new ByteArrayInputStream(resource);
			try {
				properties.load(bis);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static String get(String property) {
		String value = properties.getProperty(property);
		if (value != null) {
			value = value.trim();
		}
		return value;
	}

	public static File getJarFile(Class<?> c, String src) {
		ClassLoader loader = c.getClassLoader();
		URL url = loader.getResource(src);
		String path = url.toExternalForm();
		URI fileUri = URI.create(path);
		if (fileUri != null) {
			return new File(fileUri);
		} else {
			return null;
		}
	}

	public static InputStream getJarStream(Class<?> c, String src) {
		ClassLoader loader = c.getClassLoader();
		InputStream stream = loader.getResourceAsStream(src);

		return stream;
	}

	public static byte[] getResourceBytes(Class<?> c, String src) {
		byte[] content = null;
		List<byte[]> stream = new ArrayList<byte[]>();
		try {
			InputStream in = getJarStream(c, src);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1];
			int i = 0;
			while (in.read(buffer) > 0) {
				if (buffer[0] == '\n') {
					byte[] temp = bos.toByteArray();
					byte[] bytes = new byte[i];
					System.arraycopy(temp, 0, bytes, 0, i);
					stream.add(FeiBase64.decodeToBytes(bytes));
					stream.add(new byte[] { 13, '\n' });
					bos.flush();
					bos.reset();
					i = 0;
				} else if (buffer[0] == 13) {
				} else {
					bos.write(buffer, 0, 1);
					i++;
				}
			}
			in.close();

			int k = 0;
			for (i = 0; i < stream.size(); i++) {
				byte[] b = stream.get(i);
				if (b != null) {
					k += b.length;
				}
			}
			content = new byte[k];
			k = 0;
			for (i = 0; i < stream.size(); i++) {
				byte[] b = stream.get(i);
				if (b != null) {
					System.arraycopy(b, 0, content, k, b.length);
					k += b.length;
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;
	}

	public static String getResourceString(Class<?> c, String src) {
		String content = "";
		try {
			InputStream in = getJarStream(c, src);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1];
			int i = 0;
			while (in.read(buffer) > 0) {
				if (buffer[0] == '\n') {
					byte[] temp = bos.toByteArray();
					byte[] bytes = new byte[i];
					System.arraycopy(temp, 0, bytes, 0, i);
					content += FeiBase64.decodeToString(bytes)
							+ new String(new byte[] { 13, '\n' });
					bos.flush();
					bos.reset();
					i = 0;
				} else if (buffer[0] == 13) {
				} else {
					bos.write(buffer, 0, 1);
					i++;
				}
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;
	}

	public static void main(String[] s) {
		String content = getResourceString(FeiConfiger.class, "test1.txt");
		System.out.println(content);
	}
}
